<?php
class ControllerAccountHoahong extends Controller {
	private $error = array();

	public function index() {

		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/user', '', 'SSL');

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}

		$this->language->load('account/edit');
	
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('account/customer');
		
		if($this->customer->getId()){
			$this->data['customer_id'] = $this->customer->getId();
			$results = $this->model_account_customer->getCustomer($this->customer->getId());
			// hoa hồng trực tiếp
			$results1 = $this->model_account_customer->getAllhhBaotro($results['customer_id']);	
			// hoa hồng gián tiếp
			$results2 = $this->model_account_customer->gethhTuvan($results['customer_id']);
			$results3 = $this->model_account_customer->getTotalHoahongTructiep($results['customer_id']);
			$results4 = $this->model_account_customer->getTotalHoahongGiantiep($results['customer_id']);

			$this->data['hhtuvan'] = array();
			$this->data['hhbaotro'] = array();

			$this->data['totalbaotro'] = 0;
			$this->data['totalbaotronhan'] = 0;
			$this->data['totalbaotrochuanhan'] = 0;
			
			$this->data['totaltuvan'] = 0;
			$this->data['totaltuvannhan'] = 0;
			$this->data['totaltuvanchuanhan'] = 0;
			
			$this->data['totalnhan'] = 0;
			$this->data['totalchuanhan'] = 0;
			$this->data['total'] = 0;
			$totalbaotronhan = 0;
			$totaltuvannhan = 0;
			$totalbaotrochuanhan = 0;
			$totaltuvanchuanhan = 0;
			
			// hoa hồng trực tiếp
			if($results1){
				foreach($results1 as $result){				
				  $cus = $this->model_account_customer->getCustomer($result['customer_id']);
					if($cus){
					if((int)$result['status'] == 1 ){
						$totalbaotronhan+= $result['price'];
					}	
					
					$this->data['hhbaotro'][] = array(
							'date_added' => date('d/m/Y',strtotime($result['date_added'])),
							'date_join' => date('d/m/Y',strtotime($cus['date_added'])),
							'name' => $cus['name'],
							'madn' => $cus['madn'],
							'telephone' => $cus['telephone'],
							'email' => $cus['email'],
							'hoahong' => $this->currency->format($result['price']),
							'date_edit' => date('d/m/Y',strtotime($result['date_edit'])),
							'status' => $result['status']
						);	
				}}
				
			$this->data['totalbaotronhan']=	$this->currency->format($totalbaotronhan);
			$this->data['totalbaotro'] = $this->currency->format($results3['total']);	
			$totalbaotrochuanhan = $results3['total'] - $totalbaotronhan;
			$this->data['totalbaotrochuanhan'] = $this->currency->format($totalbaotrochuanhan);
			
			// hoa hồng gián tiếp
			if($results2){
				foreach($results2 as $result){
				$cus = $this->model_account_customer->getCustomer($result['customer_id']);
				if($cus){
					$totaltuvannhan += $result['price'];
					
					$this->data['hhtuvan'][] = array(
							'date_join' => date('d/m/Y',strtotime($cus['date_added'])),
							'date_added' => date('d/m/Y',strtotime($result['date_added'])),
							'name' => $cus['name'],
							'madn' => $cus['madn'],
							'email' => $cus['email'],
							'telephone' => $cus['telephone'],
							'date_edit' => date('d/m/Y',strtotime($result['date_edit'])),
							'status' => $result['status'],
							'hoahong' => $this->currency->format($result['price'])
						);
				}}
			}
			
			$this->data['totaltuvannhan']=	$this->currency->format($totaltuvannhan);
			$totaltuvanchuanhan = $results4['total'] - $totaltuvannhan;
			$this->data['totaltuvan']   = $this->currency->format($results4['total']);
			$this->data['totaltuvanchuanhan'] = $this->currency->format($totaltuvanchuanhan);
			
			$this->data['total'] = $this->currency->format($results4['total']+$results3['total']);
			$this->data['totalchuanhan'] = $this->currency->format($totaltuvanchuanhan+$totalbaotrochuanhan);
			$this->data['totalnhan'] = $this->currency->format($totalbaotronhan+$totaltuvannhan);
			
			$this->data['customers_id'] = $results['customer_id'];
			$this->data['name']         = $results['name'];
			$this->data['madn']         = $results['madn'];
			$this->data['money']        = $this->currency->format($results['price']);
			$this->data['money2']       = $this->currency->format($results['price1']);
			$this->data['status']       = $results['status'] ? 'Đã chuyển' : 'Chưa chuyển';
			$this->data['date_added']   = date($this->language->get('date_format_short'), strtotime($results['date_added']));
			$this->data['href']   =  $this->url->link('customer/edit');

		$this->data['back'] = $this->url->link('account/account', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/hoahong.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/hoahong.tpl';
		} else {
			$this->template = 'default/template/account/hoahong.tpl';
		}
		
		$this->children = array(

			'common/content_top_fullscreen',
			'common/content_slideshow',
			'common/content_top_fullwidth',
			'common/content_bottom_fullwidth',
			'common/content_top_two_third',
			'common/content_top_one_third_last',
			'common/icon_teaser',
			'common/header_icon_teaser',
			'common/footer_icon_teaser',
			
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
						
		$this->response->setOutput($this->render());	
	}
  }
}
public function muahang() {
	

		$this->language->load('account/edit');
	
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('account/customer');
		
		if($this->customer->getId()){
			$this->data['customer_id'] = $this->customer->getId();
			$results = $this->model_account_customer->getCustomer($this->customer->getId());
			$results1 = $this->model_account_customer->getMuaHang($results['madn']);
		
			$this->data['muahang'] = array();
			$this->data['totalmoney'] = 0;
			$this->data['totalchietkhau'] = 0;
			if($results1){
				foreach($results1 as $result){
					$this->data['totalmoney'] = $this->data['totalmoney'] + (int)$result['monney'];
					$this->data['totalchietkhau'] = $this->data['totalchietkhau'] + (int)$result['chietkhau'];	
						$this->data['muahang'][] = array(
							'date_added' => $result['date_added'],
							'name' => $result['firstname'],
							'monney' => $result['monney'],
							'chietkhau' => $result['chietkhau'],
							'details' => $result['details'],
                            'payment' => $result['payment'] 
							
						);	
					}	
				}}
			
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/muahang.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/muahang.tpl';
		} else {
			$this->template = 'default/template/account/muahang.tpl';
		}
		
		$this->children = array(

			'common/content_top_fullscreen',
			'common/content_slideshow',
			'common/content_top_fullwidth',
			'common/content_bottom_fullwidth',
			'common/content_top_two_third',
			'common/content_top_one_third_last',
			'common/icon_teaser',
			'common/header_icon_teaser',
			'common/footer_icon_teaser',
			
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
						
		$this->response->setOutput($this->render());	
	}
	public function donhang() {
		$this->language->load('account/order');
		$this->document->setTitle('Đơn hàng trên gian hàng');
		
		$this->load->model('account/customer');
		$this->load->model('account/order');
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_order_id'] = $this->language->get('text_order_id');
		$this->data['text_status'] = $this->language->get('text_status');
		$this->data['text_date_added'] = $this->language->get('text_date_added');
		$this->data['text_customer'] = $this->language->get('text_customer');
		$this->data['text_products'] = $this->language->get('text_products');
		$this->data['text_total'] = $this->language->get('text_total');
		$this->data['text_empty'] = $this->language->get('text_empty');

		$this->data['button_view'] = $this->language->get('button_view');
		$this->data['button_reorder'] = $this->language->get('button_reorder');
		$this->data['button_continue'] = $this->language->get('button_continue');
		if($this->customer->getId()){
			$this->data['customer_id'] = $this->customer->getId();
			$results = $this->model_account_customer->getCustomer($this->customer->getId());
			$results1 = $this->model_account_order->getOrderStore($results['madn']);
		
			$this->data['orders'] = array();
			$this->data['totalmoney'] = 0;
			$this->data['totalchietkhau'] = 0;
			if($results1){
				foreach($results1 as $result){
					$order_info = $this->model_account_order->getOrder($result['order_id']);
					if($order_info){
						if($this->config->get('chietkhau')) {
							$chietkhau = $this->config->get('chietkhau');
						} else {
							$chietkhau = 0;
						}
						$product_total = $this->model_account_order->getTotalOrderProductsByOrderId($result['order_id']);
						$voucher_total = $this->model_account_order->getTotalOrderVouchersByOrderId($result['order_id']);
						$this->data['orders'][] = array(
							'order_id'   => $order_info['order_id'],
							'name'       => $order_info['firstname'] . ' ' . $order_info['lastname'],
							'date_added' => date($this->language->get('date_format_short'), strtotime($order_info['date_added'])),
							'products'   => ($product_total + $voucher_total),
							'total'      => $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value']),
							'chietkhau'  => $this->currency->format(($order_info['total'] * $chietkhau / 100), $order_info['currency_code'], $order_info['currency_value']),
							'payment'    => $result['payment'],
							'date_payment'=> $result['date_payment'],
							'href'       => $this->url->link('account/order/info', 'order_id=' . $order_info['order_id'], 'SSL')
						);
					}
				}	
			}
		}
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/order_store_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/order_store_list.tpl';
		} else {
			$this->template = 'default/template/account/order_store_list.tpl';
		}
		
		$this->children = array(

			'common/content_top_fullscreen',
			'common/content_slideshow',
			'common/content_top_fullwidth',
			'common/content_bottom_fullwidth',
			'common/content_top_two_third',
			'common/content_top_one_third_last',
			'common/icon_teaser',
			'common/header_icon_teaser',
			'common/footer_icon_teaser',
			
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);
						
		$this->response->setOutput($this->render());	
	}
	public function GetCustomer() {
		$json = array();
		$this->load->model('account/customer');
		$customer_nhan = $this->model_account_customer->getCustomerByMaTuVan($this->request->post['user_nhan']);
		if(!$customer_nhan){
			$json['error'] = 'Mã người nhận không tồn tại trong hệ thống';
		}else{
			$json['success'] = $customer_nhan;
		}
		$this->response->setOutput(json_encode($json));		
  	}  
	


  	public function ChuyenDiemGiaoDich() {
		$json = array();
		$this->load->model('account/customer');
		$this->model_account_customer->addChuyenDiem($this->request->post);
		$this->response->setOutput(json_encode($json));		
  	}  
	

}
?>